package com.yuntsg.nnsfcp.service;

import com.yuntsg.nnsfcp.entity.SearchEntity;
import com.yuntsg.nnsfcp.util.WosSession;
import com.yuntsg.nnsfcp.util.httpclient.WosInfoUtil;
import com.yuntsg.nnsfcp.util.httpclient.WosTestHttpClient;
import org.apache.http.impl.client.BasicCookieStore;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.*;

public class WosService2 {

    public static void main(String[] args) {
        getHtml("hand");

    }

    public static void getHtml(String val) {
        WosSession wosSession = null;
        String select1 = "TS"; //TI是 标题  TS 主题


        String input = "hand";
        search(input, null, null, null, null,
                select1, null, null, null, null,
                null, null, null, null, wosSession);

    }

    public static void search(String input1, String input2, String input3, String input4, String input5, String
            select1, String select2, String select3, String select4, String select5, String bool12,
                              String bool23, String bool34, String bool45, WosSession wossession) {
        if (input1 == null) {
            input1 = "";
        }
        if (input2 == null) {
            input2 = "";
        }
        if (input3 == null) {
            input3 = "";
        }
        if (input4 == null) {
            input4 = "";
        }
        if (input5 == null) {
            input5 = "";
        }
        if (select1 == null) {
            select1 = "TI";
        }
        if (select2 == null) {
            select2 = "TI";
        }
        if (select3 == null) {
            select3 = "TI";
        }
        if (select4 == null) {
            select4 = "TI";
        }
        if (select5 == null) {
            select5 = "TI";
        }
        if (bool12 == null) {
            bool12 = "AND";
        }
        if (bool23 == null) {
            bool23 = "AND";
        }
        if (bool34 == null) {
            bool34 = "AND";
        }
        if (bool45 == null) {
            bool45 = "AND";
        }
        Map<String, String> params = new IdentityHashMap<String, String>();
        WosTestHttpClient client = new WosTestHttpClient();

        BasicCookieStore cookieStore = null;
//        if (session.getAttribute("woscookie") == null || session.getAttribute("session") == null ) {
        String firsturl = "http://apps.webofknowledge.com/";
        cookieStore = new BasicCookieStore();
        String firstpage = client.getHtmlWithProxy(cookieStore, firsturl);
        wossession = WosInfoUtil.getWosSession(firstpage);
        if (wossession != null) {
            wossession.setStartyear("1985");
            Calendar date = Calendar.getInstance();
            String currentYear = String.valueOf(date.get(Calendar.YEAR));
            wossession.setStopyear(currentYear);
        }
//        }
//        else
//        {
//            wossession = (WosSession)session.getAttribute("session");
//            cookieStore = (BasicCookieStore)session.getAttribute("woscookie");
//        }
        System.out.println("***********************wos下当前年份:" + wossession.getStartyear() + "---" + wossession.getStopyear());
        params.put("SID", wossession.getSid());
        params.put("SinceLastVisit_DATE", "");
        params.put("SinceLastVisit_UTC", "");
        params.put("action", "search");
        for (int i = 0; i < wossession.getEditions().size(); i++) {
            params.put(new String("editions"), wossession.getEditions().get(i));
        }
        params.put("endYear", wossession.getStopyear());
        params.put("exp_notice", "Search Error: Patent search term could be found in more than one family (unique patent number required for Expand option) ");
        params.put("fieldCount", "5");
        params.put("formUpdated", "true");
        params.put("input_invalid_notice", "Search Error: Please enter a search term.");
        params.put("input_invalid_notice_limits", " <br/>Note: Fields displayed in scrolling boxes must be combined with at least one other search field.");
        params.put("limitStatus", "collapsed");
        params.put("max_field_count", "25");
        params.put("max_field_notice", "Notice: You cannot add another field.");
        params.put("period", "Range Selection");
        params.put("product", "WOS");
        params.put("range", "ALL");
        params.put("rs_sort_by", "PY.D;LD.D;SO.A;VL.D;PG.A;AU.A");
        params.put("sa_params", "WOS||" + wossession.getSid() + "|http://apps.webofknowledge.com|'");
        params.put("search_mode", "GeneralSearch");
        params.put("ssStatus", "display:none");
        params.put("ss_lemmatization", "On");
        params.put("numDefaultGeneralSearchFields", "1");
        params.put("ss_query_language", "");
        params.put("ss_showsuggestions", "ON");
        params.put("ss_spellchecking", "Suggest");
        params.put("startYear", wossession.getStartyear());
        params.put("update_back2search_link_param", "yes");
        params.put("value(bool_1_2)", bool12);
        params.put("value(bool_2_3)", bool23);
        params.put("value(bool_3_4)", bool34);
        params.put("value(bool_4_5)", bool45);
        params.put("value(hidInput1)", "");
        params.put("value(hidInput2)", "");
        params.put("value(hidInput3)", "");
        params.put("value(hidInput4)", "");
        params.put("value(hidInput5)", "");
        params.put("value(input1)", input1);
        params.put("value(input2)", input2);
        params.put("value(input3)", input3);
        params.put("value(input4)", input4);
        params.put("value(input5)", input5);
        params.put("value(select1)", select1);
        params.put("value(select2)", select2);
        params.put("value(select3)", select3);
        params.put("value(select4)", select4);
        params.put("value(select5)", select5);
        params.put("x", String.valueOf((int) (Math.random() * 100 + 1)));
        params.put("y", String.valueOf((int) (Math.random() * 100 + 1)));
//        String secondpage = client.postHtmlWithProxy(cookieStore, "http://apps.webofknowledge.com/WOS_AdvancedSearch_input.do", params);
        String secondpage = client.postHtmlWithProxy(cookieStore, "http://apps.webofknowledge.com/WOS_GeneralSearch.do", params);

        /*
         * 保存到本地
         * */
//        File file = new File("C:\\log\\log");
//        String filename = "aaa.html";
//        FileOutputStream fileOutputStream = new FileOutputStream(file);
//        byte
//        fileOutputStream.write();


        /*
         * 结束
         * */
        Document document = Jsoup.parse(secondpage);
        Element divInfo = document.select("div.search-results").first();
        Elements infos = divInfo.select("div.search-results-item");
        List ls = new ArrayList();

        /*
         * 卷 期 页 出版年
         *
         * Volume  Issue  Pages Published
         * */
        for (int i = 0; i <= 2; i++) {
            System.out.println(infos.get(i).text());
            SearchEntity searchEntity = new SearchEntity();
            searchEntity.setTitle(infos.get(i).select("a").first().text());
            Elements spans = infos.get(i).select("span.label");
            for (int j = 0; j < spans.size(); j++) {
                if (spans.get(j).text().contains("Volume")) {
                    searchEntity.setVolume(spans.get(j).nextElementSibling().text());
                }
                if (spans.get(j).text().contains("Issue")) {
                    searchEntity.setIssue(spans.get(j).nextElementSibling().text());
                }
                if (spans.get(j).text().contains("Published")) {
                    searchEntity.setPublished(spans.get(j).nextElementSibling().text());
                }
                if (spans.get(j).text().contains("Pages")) {
                    searchEntity.setPages(spans.get(j).nextElementSibling().text());
                }
            }
            searchEntity.setAuthor(infos.get(i).select("a[title=Find more records by this author]").text());
            ls.add(searchEntity);
        }
    }

}








